/**
 * 
 */
package z.tool.util;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.Collection;
import java.util.HashMap;


/**
 * @author auzll@163.com
 */
public class ZUtils {
public static final String DEFAULT_CHARSET = "UTF-8";
    
    public static String trimStringNull(String str) {
        if (null == str) {
            return str;
        }
        str = str.trim();
        return str.length() > 0 ? str : null;
    }
    
    public static String trimString(String str) {
        if (null == str) {
            return null;
        }
        return str.trim();
    }

    public static String trimStringLowerCase(String str) {
        if (null == (str = trimString(str))) {
            return null;
        }
        return str.toLowerCase();
    }

    public static boolean isEmpty(Collection<?> collection) {
        return null == collection || 0 == collection.size();
    }

    public static boolean isEmpty(Long value) {
        return null == value || 0 == value;
    }

    public static boolean isEmpty(Object value) {
        return null == value;
    }

    public static boolean isEmpty(Integer value) {
        return null == value || 0 == value;
    }

    public static boolean isEmpty(Float value) {
        return null == value || value < 1e-6;
    }

    public static boolean isEmpty(Double value) {
        return null == value || value < 1e-6;
    }

    public static boolean isEmpty(String str) {
        if (null == (str = trimString(str))) {
            return true;
        }
        return str.length() == 0;
    }
    
    public static Long parseLong(String value) {
        value = trimStringNull(value);
        return null != value ? (value.matches("^\\d+$") ? Long.valueOf(value) : null) : null;
    }
    
    public static Integer parseInteger(String value) {
        value = trimStringNull(value);
        return null != value ? (value.matches("^\\d+$") ? Integer.valueOf(value) : null) : null;
    }
    
    public static String getNotEmptyString(String str, String defValue) {
        str = trimStringNull(str);
        return null != str ? str : defValue;
    }
    
    public static String md5(String str) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(str.getBytes(DEFAULT_CHARSET));
            StringBuilder builder = new StringBuilder();
            for (byte b: bytes) {
                builder.append(Integer.toHexString(0xf & (b >> 4))).append(
                        Integer.toHexString(0xf & b));
            }
            return builder.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String md5UpperCase(String str) {
        String res = md5(str);
        return null == res ? null : res.toUpperCase();
    }
    
    public static String convertString(Byte[] source) {
        return convertString(source, Charset.forName(DEFAULT_CHARSET));
    }
    
    public static String convertString(Byte[] source, Charset charset) {
        if (null == source || 0 == source.length) {
            return null;
        }
        
        byte[] bytes = new byte[source.length];
        
        int index = 0;
        for (Byte b : source) {
            bytes[index++] = b.byteValue();
        }
        
        return new String(bytes, charset);
    }
    
    public static String convertString(Collection<Byte> source) {
        return convertString(source, Charset.forName(DEFAULT_CHARSET));
    }
    
    public static String convertString(Collection<Byte> source, Charset charset) {
        if (null == source || 0 == source.size()) {
            return null;
        }
        
        byte[] bytes = new byte[source.size()];

        int index = 0;
        for (Byte b : source) {
            bytes[index++] = b.byteValue();
        }
        
        return new String(bytes, charset);
    }
    
    public static StringBuilder delEndStringBuilder(StringBuilder src, char c) {
        if (null != src && src.length() > 0) {
            if (c == src.charAt(src.length()-1)) {
                src.deleteCharAt(src.length()-1);
            }
        }
        return src;
    }
    
    public static String delEndString(String src, String end) {
        if (null != src && src.length() > 0 && null != end && end.length() > 0) {
            if (src.endsWith(end)) {
                if (end.length() == src.length()) {
                    return "";
                } else {
                    return src.substring(0, src.length() - end.length());
                }
            }
        }
        
        return src;
    }
    
    public static <K, V> HashMap<K, V> newHashMap() {
        return new HashMap<K, V>();
    }
    
    public static String capitalize(String s) {
        if (s.length() == 0) {
          return s;
        }
        char first = s.charAt(0);
        char capitalized = Character.toUpperCase(first);
        return (first == capitalized)
            ? s
            : capitalized + s.substring(1);
      }
}
